package cc.eddic.pratice.programmingparadigms.nginxlog.impl;

import cc.eddic.pratice.programmingparadigms.nginxlog.LogItem;
import cc.eddic.pratice.programmingparadigms.nginxlog.LogReport;
import cc.eddic.pratice.programmingparadigms.nginxlog.NginxLogItem;
import cc.eddic.pratice.programmingparadigms.nginxlog.PageViewsAndUniqueVisitors;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

public class LogReportWqjImpl implements LogReport {

    @Override
    public PageViewsAndUniqueVisitors pageViewsAndUniqueVisitors(List<NginxLogItem> items) {
        long pageViews = items.size();
        Map<String, Long> uniqueVisitorsMap = items.stream()
                .collect(Collectors.groupingBy(user -> user.getIp() + "-"
                        + user.getUserAgent(), Collectors.counting()));
        long uniqueVisitors = uniqueVisitorsMap.values().size();
        return new PageViewsAndUniqueVisitors(pageViews, uniqueVisitors);
    }

    @Override
    public Map<Integer, PageViewsAndUniqueVisitors> pageViewsAndUniqueVisitorsByHour(List<NginxLogItem> items) {
        Map<Integer, PageViewsAndUniqueVisitors> pageViewsAndUniqueVisitorsMap = new HashMap<>();
        return items.stream()
                .collect(Collectors.groupingBy(hour -> getHour(hour.getTime())))
                .entrySet().stream().map(entry -> new AbstractMap.SimpleEntry<>(
                        entry.getKey(),
                        pageViewsAndUniqueVisitors(entry.getValue())
                )).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }

    @Override
    public List<PathModel> pageViewsAndUniqueVisitorsByPath(List<LogItem> items) {


        return new ArrayList<>();
    }

    public static int getHour(String dateTimeString) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MMM/yyyy:HH:mm:ss Z", Locale.ENGLISH);
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(dateTimeString, formatter);
        return zonedDateTime.getHour();
    }

    public static String getMethod(NginxLogItem nginxLogItem) {
        String requests = nginxLogItem.getRequest();
        String[] method = requests.split(" ");
        if (method.length == 3) {
            return method[0];
        }
        return null;
    }

    public static String getUrl(NginxLogItem nginxLogItem) {
        String requests = nginxLogItem.getRequest();
        String[] url = requests.split(" ");
        if (url.length == 3) {
            return url[1];
        }
        return null;
    }
    public static String getProtocol(NginxLogItem nginxLogItem) {
        String requests = nginxLogItem.getRequest();
        String[] protocol = requests.split(" ");
        if (protocol.length == 3) {
            return protocol[2];
        }
        return null;
    }

    public static String getPath(NginxLogItem nginxLogItem) {
        if (getUrl(nginxLogItem) != null){
            String[] path = Objects.requireNonNull(getUrl(nginxLogItem)).split("\\?");
            return path[0];
        }
        return null;
    }


}

